/*
 * AdminCommandGroup.java
 *
 * Created on October 5, 2007, 11:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.io.BufferedReader;
import java.sql.*;

/**
 *
 * @author Asger
 */
public class AdminCommandGroup extends PromptingCommand {
    
    private NewRouteCommand newCmd = new NewRouteCommand(input);
    private CopyRouteCommand copyCmd = new CopyRouteCommand(input);
    private RemoveRouteCommand removeCmd = new RemoveRouteCommand(input);
    private StatisticsCommand statCmd = new StatisticsCommand(input);
    
    /** Creates a new instance of AdminCommandGroup */
    public AdminCommandGroup(BufferedReader input) {
        super(input);
    }
    
    public void setConnection(Connection connection) {
        newCmd.setConnection(connection);
        copyCmd.setConnection(connection);
        removeCmd.setConnection(connection);
        statCmd.setConnection(connection);
    }
    
    public void printHelp() {
        System.out.println("-------------------------------");
        System.out.println("Administration commands:");
        System.out.println(" new                 | Adds a new route/time");
        System.out.println(" copy                | Copies an existing route with a new departure time");
        System.out.println(" remove              | Removes an existing route/time");
        System.out.println(" stat                | Prints number of tickets sold, seats booked, and occupation % in a given period");
        System.out.println(" back                | Returns to the root menu");
        System.out.println(" help                | Show this help");
        System.out.println("-------------------------------");
    }
    
    public void execute() throws SQLException {
        printHelp();
        
        while (true)
        {
            try {
                String cmd = getInput();
                if (cmd.equals("new")) {
                    newCmd.execute();
                    printHelp();
                } else if (cmd.equals("copy")) {
                    copyCmd.execute();
                    printHelp();
                } else if (cmd.equals("remove")) {
                    removeCmd.execute();
                    printHelp();
                } else if (cmd.equals("stat")) {
                    statCmd.execute();
                } else if (cmd.equals("back")) {
                    break;
                } else {
                    System.err.println("Unknown command. Press 'help' for a list of commands.");
                }
            } catch (CancelCommandException ex) {
                printHelp();
            }
        }
    }
}



